﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using EzDesk.DesktopClient.Event;
using EzDesk.DesktopClient.FunctionWindowModule.ViewModel;
using EzDesk.EventAggregation;
using EzDesk.Generic;
using Microsoft.Practices.Unity;

namespace EzDesk.DesktopClient.FunctionWindowModule
{
    public class FunctionWindowModuleController
    {
        protected bool f_NoWindow
        {
            get
            {
                return _functionPageViewModelCollection.Count == 0;
            }
        }

        private readonly List<IFunctionPageViewModel> _functionPageViewModelCollection;
        private IActionViewModel _actionViewModel;
        private IUserManagement _userManagement;

        [InjectionConstructor]
        public FunctionWindowModuleController(IActionViewModel actionViewModel, IEnumerable<IFunctionPageViewModel> functionPageViews, IUserManagement userManagement)
        {
            FunctionNavigateEvent.Instance.Subscribe(this.OnFunctionNavigate);
            LogoutEvent.Instance.Subscribe(this.OnUserLogout);

            _actionViewModel = actionViewModel;

            _functionPageViewModelCollection = functionPageViews.ToList();

            //ToDo:Performance Fix
            _functionPageViewModelCollection.Add(new FunctionPageViewModel());

            _userManagement = userManagement;
        }

        /// <summary>
        /// @Refactory divide into another class or split into multi methods
        /// </summary>
        /// <param name="args"></param>
        public void OnFunctionNavigate(FunctionNavigateArgs args)
        {
            string targetValue = GetTargetType(args);

            //ActionRequestHandler(args);

            if (targetValue == "action")
                ActionRequestHandler(args);
            else
                PageRequestHandler(args);
        }

        protected void OnUserLogout(LogoutArgs args)
        {
            _functionPageViewModelCollection.ForEach(w => w.View.CloseWindowView());
            _functionPageViewModelCollection.Clear();
        }

        private string GetTargetType(FunctionNavigateArgs args)
        {
            var result = Regex.Match(args.Url, @"target=\w+");
            try
            {
                if (!result.Success) return "page";
                return result.Value.Split('=')[1];
            }
            catch (Exception)
            {
                return "page";
            }
        }

        private void ActionRequestHandler(FunctionNavigateArgs args)
        {
            string urlWithToken = _userManagement.GetUrlWithUserToken(args.Url);
            _actionViewModel.DoAction(urlWithToken);
        }

        private void PageRequestHandler(FunctionNavigateArgs args)
        {
            if (f_NoWindow || args.NewWindow)
            {
                var viewModel = new FunctionPageViewModel();
                _functionPageViewModelCollection.Insert(0, viewModel);
            }

            IFunctionPageViewModel functionPageViewModel = _functionPageViewModelCollection.First();

            string urlWithToken = _userManagement.GetUrlWithUserToken(args.Url);
            LogEx.TRACE("FunctionWindowModule", "Navigate to: " + urlWithToken);
            functionPageViewModel.Navigate(urlWithToken);
        }
    }
}